/*
返回与给定的前序和后序遍历匹配的任何二叉树。

pre 和 post 遍历中的值是不同的正整数

输入：pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1]
输出：[1,2,3,4,5,6,7]

*/

//Java版本递归
class Solution
{
    int[] pre, post;
public TreeNode constructFromPrePost(int[] pre, int[] post)
    {
        this.pre = pre;
        this.post = post;
        return make(0, 0, pre.length);
    }

public TreeNode make(int i0, int i1, int N)
    {
        if (N == 0)
            return null;
        TreeNode root = new TreeNode(pre[i0]);
        if (N == 1)
            return root;

        int L = 1;
        for (; L < N; ++L)
            if (post[i1 + L - 1] == pre[i0 + 1])
                break;

        root.left = make(i0 + 1, i1, L);
        root.right = make(i0 + L + 1, i1 + L, N - 1 - L);
        return root;
    }
}

//递归
class Solution
{
    int pre, post;
    TreeNode constructFromPrePost()
    {
    }
};
